<?php

namespace app\common\model;

use addons\shopro\model\User;
use addons\shopro\model\Order;
use addons\shopro\model\OrderItem;
use app\admin\model\share\Money as ShareMoney;
use think\Db;
use app\api\model\OrderM;
use app\api\model\OrderSm;

class Share
{
    /**
     * 用户下单奖励
     */
    public function add_share_order($order_id, $goods_id, $user_id)
    {
        $user = User::get($user_id);
        if (!$user) {
            return false;
        }
        
        if (empty($goods_id)) {
            return 0;
        }
        $shopro_order = (new Order())->where(['id' => $order_id])->find();
        if (empty($shopro_order)) {
            return 0;
        }
        if ($shopro_order['status'] != 1) {
            return 0;
        }

        if (!in_array($goods_id, array(config('site.hyspid'),config('site.dlspid')))) {
            return 0;
        }

        //用户等级
        if ($goods_id == config('site.hyspid')) {
            if ($user->grade == 2) {
                $user->grade = 2;
                $user->number = -1;
                $user->grade_time = date('Y-m-d H:i:s',strtotime("+1 year"));
            } else {
                $user->grade = 1;
                $user->number = -1;
                $user->grade_time = date('Y-m-d H:i:s',strtotime("+3 months"));
                // if ($user->grade == 1 && $user->number > 0) {
                //     $user->number += config('site.hymfcs');
                // } else {
                //     $user->number = config('site.hymfcs');
                // }
            }
        } else {
            $user->grade = 2;
            $user->number = -1;
            $user->grade_time = date('Y-m-d H:i:s',strtotime("+1 year"));
        }

        //$user->grade_time = date('Y-m-d H:i:s',strtotime("+1 year"));
        $user->save();

        $parent_user =  User::get($user->parent_user_id);
        if (!$parent_user) {
            return false;
        }

        $total_fee = $shopro_order['total_fee'];
        // if ($goods_id == config('site.hyspid')) {
        //     $total_fee = 98;
        // } else {
        //     $total_fee = 980;
        // }
        
        // 直推奖励
        if($parent_user->grade > 0){
            if ($parent_user->grade == 1) {
                $money = bcmul(config('site.hyzt'),$total_fee, 2);
                $type = 1;
            } elseif ($parent_user->grade == 3) {
                $money = bcmul(config('site.fxzt'),$total_fee, 2);
                $type = 5;
            } else {
                $money = bcmul(config('site.dlzt'),$total_fee, 2);
                $type = 2;
            }
            if ($money > 0) {
                $this->grant($money,$parent_user->id,$order_id,$type,3);
            }
        }

        if ($parent_user->grade != 2) {
            // 代理间推奖励
            $this->add_range( $order_id, $parent_user->parent_user_id,$total_fee);
        }

        return 1;
    }

    /**
     * 间推奖励
     * parent_id  上级的上级id
     */
    private function add_range( $order_id, $parent_id,$total_fee)
    {
        $user = User::get($parent_id);
        if (!$user) {
            return false;
        }

        if ($user->grade == 2) {
            $money = bcmul(config('site.dljt'),$total_fee, 2);
            if ($money > 0) {
                $this->grant($money,$user->id,$order_id,3,3);
            }
            return 0;
        }

        return $this->add_range($order_id, $user->parent_user_id, $total_fee);
    }

     /**
     * 间推奖励
     * parent_id  上级的上级id
     */
    public function add_goods_range($order_id, $user_id, $isZi = true, $classify = 0)
    {
        if ($isZi)
            $ddgm = config('site.ddgm');//19.8
        else
            $ddgm = config('site.wxcyPrice');

        $temp_user = User::get($user_id);
        if (!$temp_user) {
            return false;
        }
        $user = User::get($temp_user->parent_user_id);
        if (!$user) {
            return false;
        }
        //直推奖励
        if($user->grade > 0){
            if ($user->grade == 1) {
                $money = bcmul(config('site.hyzt'),$ddgm, 2);
                $type = 1;
            } elseif ($user->grade == 3) {
                $money = bcmul(config('site.fxzt'),$ddgm, 2);
                $type = 5;
            } else {
                $money = bcmul(config('site.dlzt'),$ddgm, 2);
                $type = 2;
            }
            if ($money > 0) {
                $this->grant($money,$user->id,$order_id,$type,$classify);
            }
        }
        if($user->grade == 2){
            return 1;
        }

        //间推奖励
        $this->goods_range($order_id, $user->parent_user_id, $isZi,$classify);
    }

    private function goods_range($order_id, $parent_id, $isZi = true,$classify)
    {
        $user = User::get($parent_id);
        if (!$user) {
            return false;
        }

        if ($user->grade == 2) {
            $ddgm = config('site.ddgm');//19.8
            $money = bcmul(config('site.dljt'),$ddgm, 2);
            if ($money > 0) {
                $this->grant($money,$user->id,$order_id,3,$$classify);
            }
            return 0;
        }

        return $this->goods_range($order_id, $user->parent_user_id, $isZi,$classify);
    }

     /**
     * 佣金发放
     * classify 1 zzgood_order 2 store_ordersm 3 shopro_order
     */
    public function grant($money,$user_id,$order_id,$type,$classify = 0)
    {
        $money = bcdiv($money,100,2);
        if ($money <= 0) {
            return 0;
        }
        $memo = '';
        switch ($type) {
            case 1:
                $memo = '会员邀请奖励';
                break;
            case 2:
                $memo = '代理邀请奖励';
                break;
            case 3:
                $memo = '代理间推奖励';
                break;
            case 4:
                $memo = '代理推荐奖励';
                break;
            case 5:
                $memo = '分销邀请奖励';
                break;
        }
        \addons\shopro\model\User::money($money, $user_id, 'commission_income', $order_id, $memo,[
            'classify' => $classify
        ]);
    }

    /**
     * 用户次数减少
     */
    public function reduce_number($user_id,$xing, $ming,$info)
    {
        $user = User::get($user_id);
        if (empty($user) || $user->grade != 1 || $user->number <= 0) {
            return false;
        } else {
            $user->number = $user->number - 1;
            $user->save();
            $order  = OrderM::create([
                'payed_is'  => OrderM::PAYED_IS_YES,
                'user_id'   => $user_id,
                'sn'        => self::getSn($user['id']),
                'xing'      => $xing,
                'ming'      => $ming,
                'info'      => json_encode($info),
                'content'   => '',
                'total_fee' => 0
            ]);
        }
    }

    // 获取订单号
    public static function getSn($user_id)
    {
        $rand = $user_id < 9999 ? mt_rand(100000, 99999999) : mt_rand(100, 99999);
        $order_sn = date('Yhis') . $rand;

        $id = str_pad($user_id, (24 - strlen($order_sn)), '0', STR_PAD_BOTH);

        return $order_sn . $id;
    }

    /**
     * 分配账号发放
     * type 1 zzgood_order 2 store_ordersm 3 shopro_order
     */
    public function add_distribution($order_id, $type)
    {
        $fpzh = config('site.fpzh');
        $user = User::getByMobile($fpzh);
        $fpbl = config('site.fpbl');
        if (!$user || !$fpbl) {
            return;
        }
        $money = 0;
        if ($type == 1) {
            $order = (new OrderM())->where(['id' => $order_id])->find();
            if ($order->payed_is == 'yes' && $order->total_fee > 0) {
                $money = bcmul($fpbl, $order->total_fee, 2);
                $memo = '测字订单分成';
            }
        } else if($type == 2){
            $order = (new OrderSm())->where(['id' => $order_id])->find();
            if ($order->status == 1 && $order->pay_price > 0) {
                $money = bcmul($fpbl, $order->pay_price, 2);
                $memo = '五行穿衣分成';
            }
        } else if($type == 3){
            $order = (new Order())->where(['id' => $order_id])->find();
            if ($order->status == 1 && $order->total_fee > 0 &&  $order->pay_type == 'wechat') {
                $money = bcmul($fpbl, $order->total_fee, 2);
                $memo = '会员购买分成';
            }
        } else {
            return;
        }
        $money = bcdiv($money,100,2);
        if ($money > 0) {
            \addons\shopro\model\User::money($money, $user->id, 'divide_into', $order_id, $memo,[
                'type' => $type
            ]);
        }
    }

}
